/**
 * Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */
package com.mbfasttrack.controller;

import com.mbfasttrack.model.Hardware;
import com.mbfasttrack.service.HardwareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author Marco Berri marcoberri@gmail.com http://tecnicume.blogspot.com
 */
@Controller
public class HardwareController extends Base {

    /**
     *
     * @return
     */
    @ModelAttribute("hardware")
    public Hardware getHardwareObject() {
        return new Hardware();
    }

    /**
     *
     * @param commentService
     */
    @Autowired(required = true)
    public void setHardwareService(HardwareService hardwareService) {
        this.hardwareService = hardwareService;

    }

    /**
     *
     * @param comment
     * @param model
     * @return
     */
    @RequestMapping(value = "/hardware/save", method = RequestMethod.POST)
    public String save(@ModelAttribute("hardware") Hardware hardware, Model model) {

        if (isGuest()) {
            return "redirect:/login";
        }

        org.springframework.security.core.userdetails.User u = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        hardware = this.hardwareService.add(hardware, u.getUsername());



        hardwareService.save(hardware);



        model.addAttribute("hardware", hardware);
        return "response/ok";

    }
}